import api from "@/assets/js/api.js"

let newState = function () {
  return {
    marketOptions:[],
    shopOptions:[],
    brandOptions:[],
    seriesOptions:[],
    modeOptions:[],
    carTypeOptions:[],
    colorOptions:[],
    provinceOptions:[],
    cityOptions:[],
    brandsOptions: [],
    platesOptions:[{name:'京'},{name:'津'},{name:'冀'},{name:'晋'},{name:'蒙'},{name:'辽'},{name:'吉'},{name:'黑'},{name:'浙'},{name:'皖'},{name:'闽'},{name:'赣'},{name:'鲁'},{name:'豫'},{name:'鄂'},{name:'湘'},{name:'粤'},{name:'桂'},{name:'琼'},{name:'川'},{name:'贵'},{name:'云'},{name:'渝'},{name:'藏'},{name:'陕'},{name:'甘'},{name:'青'},{name:'宁'},{name:'新'},{name:'港'},{name:'澳'},{name:'台'},],
    platesEnOptions:[{name:'A'},{name:'B'},{name:'C'},{name:'D'},{name:'E'},{name:'F'},{name:'G'},{name:'H'},{name:'I'},{name:'J'},{name:'K'},{name:'L'},{name:'M'},{name:'N'},{name:'O'},{name:'P'},{name:'Q'},{name:'R'},{name:'S'},{name:'T'},{name:'U'},{name:'V'},{name:'W'},{name:'X'},{name:'Y'},{name:'Z'}],
    UNICODESiderOptions: [
      {id: '07240', code: '&#xe61e;'},
      {id: '42841', code: '&#xe620;'},
      {id: '46297', code: '&#xe61d;'},
      {id: '46689', code: '&#xe615;'},
      {id: '20693', code: '&#xe613;'},
      {id: '10971', code: '&#xe619;'},
      {id: '94466', code: '&#xe61b;'},
      {id: '05390', code: '&#xe612;'},
      {id: '40357', code: '&#xe621;'},
      {id: '34952', code: '&#xe614;'},
      {id: '73595', code: '&#xe618;'},
      {id: '38563', code: '&#xe611;'},
      {id: '32580', code: '&#xe61a;'},
      {id: '07267', code: '&#xe617;'},
      {id: '57253', code: '&#xe61c;'},
      {id: '28938', code: '&#xe616;'},
      {id: '46059', code: '&#xe61f;'},
      {id: '68858', code: '&#xe610;'},
    ],
    carBaseParamOptions: {
      baseCarEnvironOptions: [],
      baseCarFuelOptions: [],
      baseCarGearboxOptions: [],
      baseCarInColorOptions: [],
      baseCarOutColorOptions: [],
      baseCarPurposeOptions: [],
      baseCarTypeOptions: [],
    },
    orderSaleTypeOptions: [
      {name: '批发', value: 1},
      {name: '直销', value: 2},
      {name: '拍卖', value: 3}
    ],
    salePayTypeOptions: [
      {name: '现款', value: 1},
      {name: '按揭', value: 2}
    ]
  }
}

export default {
  state: new newState(),
  mutations:{
    // 重置
    reset (state) {
      this.state.common = new newState()
    },
    //查询市场
    getMarketOptions(state,val){
      if(state.marketOptions.length>0){
        if(val.insertMarket){
          window.axios.get(api.getMarketList,{
            params:{
              marketId:this._vm.$sess.get('userInfo').marketId
            }
          }).then((res)=>{
            if(res){
              if (val.isbool){
                state.marketOptions = val.marketOptions = res.data.result
                val.marketOptions = [{ "marketName": "全部市场", "marketId": "" }, ...val.marketOptions]
              }else{
                state.marketOptions = val.marketOptions = res.data.result
              }
              return
            }
          })
        }
        if (val.isbool) {
          val.marketOptions = state.marketOptions
          val.marketOptions = [{ "marketName": "全部市场", "marketId": "" }, ...val.marketOptions]
        } else {
          val.marketOptions = state.marketOptions
        }
        return
      }else{
        window.axios.get(api.getMarketList,{
          params:{
            marketId:this._vm.$sess.get('userInfo').marketId
          }
        }).then((res)=>{
          if(res){
            if (val.isbool){
              state.marketOptions = val.marketOptions = res.data.result
              val.marketOptions = [{ "marketName": "全部市场", "marketId": "" }, ...val.marketOptions]
            }else{
              state.marketOptions = val.marketOptions = res.data.result
            }
            return
          }
        })
      }
    },

    //查询车商
    getShopOptions(state,val){
      if(val.params.marketId){
        window.axios.get(api.getShopList,{
          params:{
            marketId:val.params.marketId,
            shopId: this._vm.$sess.get('userInfo').shopId || '',
            shopState:1,
          }
        }).then((res)=>{
          if(res){
            val.shopOptions = res.data.result;
            return
          }
        })
      }else{
        return
      }
    },
    //查询区域
    getRegionOptions(state,val){
      if(val.params.marketId){
        window.axios.get(api.getRegionList,{
          params:{
            marketId:val.params.marketId,
            shopState:1,
          }
        }).then((res)=>{
          if(res){
            state.regionOptions = val.regionOptions = res.data.result
            return
          }
        })
      }else{
        return
      }
    },
    // 查询用户列表
    getUserOptions(state,val){
      if(val.params.marketId){
        window.axios.get(api.getUserList,{
          params:{
            marketId:val.params.marketId,
            shopId:val.params.shopId,
          }
        }).then((res)=>{
          if(res){
            state.userOptions = val.userOptions = res.data.result
            return
          }
        })
      }else{
        return
      }
    },    
    // 查询品牌带分类
    getBrandsOptions(state,val){
      
      if(state.brandOptions.length<=0){
        window.axios.get(api.getGroupBrand).then((res)=>{
          if(res){
            state.brandOptions = []
            val.brandOptions = state.brandOptions = res.data.result
            return
          }
        })
      }else{
        val.brandOptions = state.brandOptions
        return
      }
    },
    //查询车系
    getSeriesOptions(state,val){

      if(val.params.brandId){
        window.axios.get(api.getSeriesList,{params:{
            brandId:val.params.brandId
          }}).then((res)=>{
          if(res){
            // console.log(res, '111111111111111111');
            state.seriesOptions = res.data.result
            val.seriesOptions = state.seriesOptions
          }
        })
      }
    },
    //查询车型
    getModeOptions(state,val){
      if(val.params.seriesId){
        window.axios.get(api.getModelList,{params:{
            seriesId:val.params.seriesId
          }}).then((res)=>{
          if(res){
            state.modeOptions = res.data.result
            val.modeOptions = state.modeOptions
          }
        })
      }
    },
    
   
    //查询省列表

    getProvinceOptions(state,val){
      if(state.provinceOptions.length<=0){
        window.axios.get(api.province).then((res)=>{
          if(res){
						// console.log(res.data.result)
            state.provinceOptions = val.provinceOptions = res.data.result
            return
          }
        })
      }else{
        val.provinceOptions = state.provinceOptions
        return
      }
    },
    
    // 查询市列表
    getCityOptions(state,val){
      // console.log(val, '1111111111112222222222222');
      if(val.provinceId){
        window.axios.get(api.city,{params:{provinceId:val.provinceId}}).then((res)=>{
          if(res){
            // console.log(res.data.result, '111111111111111111');
            state.cityOptions = val.cityOptions = res.data.result
            return
          }
        })
      }else{
        val.cityOptions = state.cityOptions
      }
    },
    // 基础选项
    getBaseParamOptions(state,val){
      if (!state.carBaseParamOptions.baseCarEnvironOptions.length) {
        window.axios.get(api.getCarBaseParam).then((res)=>{
          if(res){
            state.carBaseParamOptions = res.data.result
            // console.log(state.carBaseParamOptions)
            typeof val === typeof {} && (val.carBaseParamOptions = state.carBaseParamOptions)
            return
          }
        })
      }else{
        // console.log(state.carBaseParamOptions)
        typeof val === typeof {} && (val.carBaseParamOptions = state.carBaseParamOptions)
        return
      }
    }
  }
}
